package gdut.imis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gdut.imis.Utils.Result;
import gdut.imis.domain.Classes;
import gdut.imis.enums.Status;
import gdut.imis.mapper.ClassesMapper;
import gdut.imis.service.ClassesService;
import gdut.imis.vo.ClassesVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description class服务层实现类
 * @Author JiaFu
 * @Date 2023/4/7 14:00
 */
@Service
@RequiredArgsConstructor
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    private final ClassesMapper classesMapper;

    /***
     * 新增班级档案(拥有classes权限)
     * @param: classes
     * @return Result
     */
    @Override
    public Result addClass(Classes classes) {
        if (Objects.isNull(classes)) {
            return Result.errorWithArgs(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "class");
        }
        //查看是否已有当前班级存在
        LambdaQueryWrapper<Classes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Classes::getMajorId, classes.getMajorId())
                .eq(Classes::getGrade, classes.getGrade())
                .eq(Classes::getName, classes.getName());
        Classes one = this.getOne(queryWrapper);
        if (one != null) {
            return Result.errorWithArgs(Status.FAILED, "该专业下该年级该班级已存在");
        }
        boolean flag = this.save(classes);
        if (flag) {
            return Result.success();
        }
        return Result.errorWithArgs(Status.FAILED, "新增班级失败");
    }

    /***
     * 根据专业id查询班级列表
     * @param: majorId
     * @return Result
     */
    @Override
    public Result getClassListByMajorId(Long majorId) {
        LambdaQueryWrapper<Classes> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Classes::getMajorId,majorId);
        List<Classes> classesList = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(classesList)) {
            return Result.errorWithArgs(Status.FAILED,"没有查询到任何班级信息");
        }
        return Result.success(classesList);
    }

    /***
     * 根据学生id查询学生班级、专业、班主任、学校信息
     * @param: studentId
     * @return Result
     */
    @Override
    public Result getClassesInfoByStuId(Long studentId) {
        ClassesVo classesVo = classesMapper.getClassesInfoByStuId(studentId);
        if (classesVo != null) {
            return Result.success(classesVo);
        }
        return Result.errorWithArgs(Status.FAILED,"查询学生班级信息失败");
    }
}
